package com.example.dmgame.adapter;

import java.util.List;

import com.example.dmgame.R;
import com.example.dmgame.bean.GameContent;
import com.example.dmgame.utils.AsyncBitmapLoader;
import com.example.dmgame.utils.AsyncBitmapLoader.ImageCallBack;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class GameAdapter extends BaseAdapter {

	List<GameContent> list;
	Context context;
	GridView gridView;
//	AsyncImageLoader asyncImageLoader;
	AsyncBitmapLoader asyncBitmapLoader;

	public GameAdapter(List<GameContent> list, Context context,GridView gridView) {
		super();
		this.list = list;
		this.context = context;
		this.gridView = gridView;
		//asyncImageLoader = new AsyncImageLoader();
		asyncBitmapLoader = new AsyncBitmapLoader();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if(convertView==null){
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.game_item, null);
			viewHolder.gameImageView = (ImageView)convertView.findViewById(R.id.game_img);
			viewHolder.gameName = (TextView)convertView.findViewById(R.id.gameName);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		GameContent gameContent = list.get(position);
		viewHolder.gameName.setText(gameContent.getTitle());
		Log.i("GameAdapter", "---->>title:"+gameContent.getTitle());
		String imageUrl = "http://www.3dmgame.com"+gameContent.getLitpic();
		viewHolder.gameImageView.setTag(imageUrl);
		
//		Drawable cacheImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
//			
//			@Override
//			public void imageLoaded(Drawable imageDrawable, String imageUrl) {
//				// TODO Auto-generated method stub
//				ImageView imageViewByTag = (ImageView)gridView.findViewWithTag(imageUrl);
//				if(imageViewByTag!=null){
//					imageViewByTag.setImageDrawable(imageDrawable);
//				}
//			}
//		});
		Bitmap bitmap = asyncBitmapLoader.loadBitmap(viewHolder.gameImageView, imageUrl, new ImageCallBack() {
			
			public void imageLoad(ImageView imageView, Bitmap bitmap) {
				// TODO Auto-generated method stub
				imageView.setImageBitmap(bitmap);
			}
		});
		if(bitmap==null){
			viewHolder.gameImageView.setImageResource(R.drawable.product_default);
		}else{
			viewHolder.gameImageView.setImageBitmap(bitmap);
		}
		return convertView;
	}
	
	private final class ViewHolder{
		ImageView gameImageView;
		TextView gameName;
	}

}
